﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class ViewAlbum : System.Web.UI.Page
{
    string connectionString;
    private SqlConnection con;
    private SqlDataAdapter da;
    private SqlCommand cmd;
    private DataSet ds;

    string Imageurl = "";
    int accountID = 0;
    int price = 0;
    int Qty = 0, OderID = 0, OderDID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString = WebConfigurationManager.ConnectionStrings["myCon"].ToString();
        con = new SqlConnection(connectionString);
        con.Open();

        if (!IsPostBack)
        {
            loadData(Session["User"].ToString());
        }
    }

    private void loadData(string userName)
    {
        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spViewAlbum", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@UserName",SqlDbType.VarChar).Value = userName;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds, "Image");
            GridView1.DataSource = ds.Tables["Image"];
            if (ds.Tables["Image"].Rows.Count == 0)
            {
                lblStatus1.Visible = true;
                lblStatus2.Visible = true;
                hylblStatus.Visible = true;
            }
            else
            { 
                GridView1.DataBind();
                ds = new DataSet();
                cmd = new SqlCommand("spGetAllRes_Price", con);
                cmd.CommandType = CommandType.StoredProcedure;
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "Res");
                int a = ds.Tables["Res"].Rows.Count;
                dropDownListRes.Items.Clear();
                for (int i = 0; i < a; i++)
                {
                    dropDownListRes.Items.Add(ds.Tables["Res"].Rows[i][0].ToString());
                }
            }
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {


    }

    protected void getAccountID(string account)
    {
        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spGetAccount", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@userName", SqlDbType.VarChar).Value = account;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds, "Account");
            accountID = Int32.Parse(ds.Tables["Account"].Rows[0][0].ToString());

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }
    protected void getPrice(string res)
    {
        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spGetPricebyRes", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@RES", SqlDbType.VarChar).Value = res;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds, "PricebyRes");
            price = Int32.Parse(ds.Tables["PricebyRes"].Rows[0][1].ToString());

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }
    protected void getImageUrl(int ID)
    {
        
        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spGetImageUrl", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ID;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds, "ImageUrl");
            Imageurl = ds.Tables["ImageUrl"].Rows[0][0].ToString();
            
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }

    protected void getInsertOrderDetails(int OrderID, string url, string res, int total)
    {

        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spAddOrderDetails", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@OrderID", SqlDbType.Int).Value = OrderID;
            cmd.Parameters.Add("@ImgUrl", SqlDbType.VarChar).Value = url;
            cmd.Parameters.Add("@RES", SqlDbType.VarChar).Value = res;
            cmd.Parameters.Add("@Quantity", SqlDbType.Int).Value = 1;
            cmd.Parameters.Add("@TotalPrice", SqlDbType.Int).Value = total;
            cmd.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }
    protected void getInsertOrder(int CustomerID)
    {

        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spAddOrder", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@CustomerID", SqlDbType.Int).Value = CustomerID;
            cmd.Parameters.Add("@Status", SqlDbType.VarChar).Value = "Carting";
            cmd.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }
    protected bool checkOrderDetails(int CusID, string imgUrl, string res)
    {
        bool check = false;
        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spGetAllOrderByCusID", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@CusID", SqlDbType.Int).Value = CusID;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds, "checkOrderDetails");
            int a = ds.Tables["checkOrderDetails"].Rows.Count;
            if(a == 0){
                check = false;
            }
            else if(a > 0 ){
                for (int i = 0; i < a; i++)
                {
                    if (ds.Tables["checkOrderDetails"].Rows[i][4].ToString() == imgUrl && ds.Tables["checkOrderDetails"].Rows[i][5].ToString() == res)
                    {
                        check = true;
                        Qty = Int32.Parse(ds.Tables["checkOrderDetails"].Rows[i][6].ToString());
                        OderID = Int32.Parse(ds.Tables["checkOrderDetails"].Rows[i][0].ToString());
                        OderDID = Int32.Parse(ds.Tables["checkOrderDetails"].Rows[i][2].ToString());
                        break;
                    }
                    else
                    {
                        check = false;
                    }
                }
            }
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            con.Close();
        }
        return check;

    }
    protected int getOrderID(int CusID)
    {
        int OrderId = 0;
        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spGetOrderIDbyCusIDAssending", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@CusID", SqlDbType.Int).Value = CusID;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            OrderId = Int32.Parse(ds.Tables[0].Rows[0][0].ToString());
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            con.Close();
        }
        return OrderId;

    }
    protected void updateOrderDetails(int OID, int ODID, int Quantity, int Price)
    {
        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spUpdateOrderDetails", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@OrderID", SqlDbType.Int).Value = OID;
            cmd.Parameters.Add("@OderDetailsID", SqlDbType.Int).Value = ODID;
            cmd.Parameters.Add("@Qty", SqlDbType.Int).Value = Quantity;
            cmd.Parameters.Add("@Price", SqlDbType.Int).Value = Price;
            cmd.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string res = dropDownListRes.Text;
        

        GridViewRow row = GridView1.SelectedRow;
        getImageUrl(Int32.Parse(row.Cells[0].Text));
        getAccountID(Session["User"].ToString());
        getPrice(res);
        int id = accountID;
        int priceCart = price;
        string url = Imageurl;
        if (checkOrderDetails(id, url, res))
        {
            Qty = Qty + 1;
            priceCart = priceCart * Qty;
            updateOrderDetails(OderID, OderDID, Qty, priceCart);
        }
        else
        {

            getInsertOrder(id);
            int OderID = getOrderID(id);
            getInsertOrderDetails(OderID, url, res, priceCart);
            lblStatus3.Text = lblStatus3.Text = "Add imgurl: " + url + " res: " + res + " account ID: " + id + " Price: " + priceCart;
        }
        
    }
}